//author:kakake
class tsschedualService {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    constructor(workid: number) {
        this.data.WorkId = workid;
    }

    /**
     * 计算两个日期，返回日期数组
     */
    public calculationdate(sdate, edate) {

        var datelist = [];

        let date1 = new Date(sdate);
        let date2 = new Date(edate);

        //calculate time difference
        var time_difference = date2.getTime() - date1.getTime();

        //calculate days difference by dividing total milliseconds in a day
        var days_difference = time_difference / (1000 * 60 * 60 * 24);
        datelist.push(sdate);
        for (var i = 1; i <= days_difference; i++) {
            var dateTime = new Date(sdate);
            var mdate = new Date(dateTime.setDate(dateTime.getDate() + i));
            datelist.push(TSLobsterAPI.Utils.dateFormat(mdate, 'yyyy-MM-dd'));
        }

        return datelist;
    }

    /**
     * 判断是否已排班
     */
    existschedual(date, deptid, doctid, timerange, schedtype) {
        let strsql = 'select count(1) from WPMT_DocSchedual where SchedType=@schedtype and SchedualDate=@date and DeptId=@deptid and DoctID=@doctid and TimeRange=@timerange and WorkId=@workid';
        let dbparam = {
            date: date,
            deptid: deptid,
            doctid: doctid,
            timerange: timerange,
            schedtype: schedtype,
            workid: this.data.WorkId
        }

        return TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparam) > 0;
    }

    generateschedual(model) {

        var datelist = this.calculationdate(model.sdate, model.edate);

        let docschedual_orm = new WPMT_DocSchedual_ORM(this.data.WorkId);
        datelist.forEach(item => {
            //上午
            if (model.am_num > 0) {
                var docschedual: WPMT_DocSchedual_Model = <WPMT_DocSchedual_Model>{};
                docschedual.SchedualID = 0;
                docschedual.DeptID = model.deptId;
                docschedual.DoctID = model.doctId;
                docschedual.SchedualDate = item;
                docschedual.TimeRange = 1;
                docschedual.Flag = 0;
                docschedual.AppointNum = model.am_num;
                docschedual.LimitNum = model.am_num;
                docschedual.RoomID = 0;
                docschedual.SchedType = model.SchedType;
                if (this.existschedual(item, docschedual.DeptID, docschedual.DoctID, docschedual.TimeRange, docschedual.SchedType) == false) {
                    docschedual_orm.Save(docschedual);
                }
            }
            //下午
            if (model.pm_num > 0) {
                var docschedual: WPMT_DocSchedual_Model = <WPMT_DocSchedual_Model>{};
                docschedual.SchedualID = 0;
                docschedual.DeptID = model.deptId;
                docschedual.DoctID = model.doctId;
                docschedual.SchedualDate = item;
                docschedual.TimeRange = 2;
                docschedual.Flag = 0;
                docschedual.AppointNum = model.pm_num;
                docschedual.LimitNum = model.pm_num;
                docschedual.RoomID = 0;
                docschedual.SchedType = model.SchedType;
                if (this.existschedual(item, docschedual.DeptID, docschedual.DoctID, docschedual.TimeRange, docschedual.SchedType) == false) {
                    docschedual_orm.Save(docschedual);
                }
            }

            //晚上
            if (model.nm_num > 0) {
                var docschedual: WPMT_DocSchedual_Model = <WPMT_DocSchedual_Model>{};
                docschedual.SchedualID = 0;
                docschedual.DeptID = model.deptId;
                docschedual.DoctID = model.doctId;
                docschedual.SchedualDate = item;
                docschedual.TimeRange = 3;
                docschedual.Flag = 0;
                docschedual.AppointNum = model.nm_num;
                docschedual.LimitNum = model.nm_num;
                docschedual.RoomID = 0;
                docschedual.SchedType = model.SchedType;
                if (this.existschedual(item, docschedual.DeptID, docschedual.DoctID, docschedual.TimeRange, docschedual.SchedType) == false) {
                    docschedual_orm.Save(docschedual);
                }
            }
        })

        return { result: true };

    }
}

interface WPMT_DocSchedual_Model {
    "SchedualID": number,
    "DeptID": number,
    "DoctID": number,
    "SchedualDate": string,
    "TimeRange": number,
    "Flag": number,
    "AppointNum": number,
    "LimitNum": number,
    "RoomID": number,
    "WorkId": number,
    "SchedType": number,
}

class WPMT_DocSchedual_ORM {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    constructor(workid: number) {
        this.data.WorkId = workid;
        this.WPMT_DocSchedual.WorkId = workid;
    }

    public WPMT_DocSchedual = {
        "model": {
            "SchedualID": 0,
            "DeptID": 0,
            "DoctID": 0,
            "SchedualDate": "yyyy-MM-dd",
            "TimeRange": 0,
            "Flag": 0,
            "AppointNum": 0,
            "LimitNum": 0,
            "RoomID": 0,
            "WorkId": 0,
            "SchedType": 0
        },
        "tablename": "WPMT_DocSchedual",
        "key": "SchedualID",
        "val": "0",
        "objval": {},
        "query": {},
        "where": "",
        "pageinfo": {},
        "WorkId": 1
    }

    Save(model: WPMT_DocSchedual_Model): any {
        this.WPMT_DocSchedual.objval = model;
        return TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.WPMT_DocSchedual);
    }

    GetList(where?: string) {
        return TSLobsterAPI.OrmMsSql.GetListModel(this.data.dbConn, this.WPMT_DocSchedual);
    }
}
